﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;

namespace BLL
{
  public class SlideBLL
  {
    public List<Slide> GetAll()
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Slides.OrderBy(p => p.Sort).ToList();
      }
    }

    public List<Slide> GetAll(bool isEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Slides.Where(p => p.isEnable == isEnable).OrderBy(p => p.Sort).ToList();
      }
    }

    public List<Slide> GetByPosition(int position)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Slides.Where(p => p.Position == position).OrderBy(p => p.Sort).ToList();
      }
    }
    public List<Slide> GetByPosition(int position, bool isEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Slides.Where(p => p.Position == position && p.isEnable == isEnable).OrderBy(p => p.Sort).ToList();
      }
    }

    public bool CheckExist(int id)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Slide obj =  data.Slides.Where(p => p.Id == id).FirstOrDefault();
        if (obj == null) return false;
        else return true;
      }
    }

    public int GetCountItem()
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Slides.Count();
      }
    }


    //Get by id

    public Slide GetById(int id)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Slides.Where(p => p.Id == id).FirstOrDefault();
      }
    }

    public Slide GetById(int id, bool isEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Slides.Where(p => p.Id == id && p.isEnable == isEnable).FirstOrDefault();
      }
    }

    //Get max Sort

    public byte GetMaxSort(int position)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Slide obj = data.Slides.Where(p => p.Position == position).OrderByDescending(p => p.Sort).FirstOrDefault();

        if (obj == null) return 1;
        else return (byte)(obj.Sort + 1);
      }
    }

    //Swap sort

    public void SwapSort(int idFirst, int idSecond)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Slide objFirst = data.Slides.Where(p => p.Id == idFirst).FirstOrDefault();
        Slide objSecond = data.Slides.Where(p => p.Id == idSecond).FirstOrDefault();

        int tg = objFirst.Sort;
        objFirst.Sort = objSecond.Sort;
        objSecond.Sort = tg;

        data.SubmitChanges();
      }
    }

    //Enable

    public void Enable(int id)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Slide obj = data.Slides.Where(p => p.Id == id).FirstOrDefault();
        obj.isEnable = !obj.isEnable;

        data.SubmitChanges();
      }
    }

    public void Enable(int id, bool isEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Slide obj = data.Slides.Where(p => p.Id == id).FirstOrDefault();
        obj.isEnable = isEnable;

        data.SubmitChanges();
      }
    }

    //Add

    public void Add(Slide obj)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        data.Slides.InsertOnSubmit(obj);
        data.SubmitChanges();
      }
    }

    //Update 

    public void Update(Slide obj)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Slide slide = data.Slides.Where(p => p.Id == obj.Id).FirstOrDefault();
        slide.isEnable = obj.isEnable;
        slide.Sort = obj.Sort;
        slide.Position = obj.Position;
        data.SubmitChanges();
      }
    }

    //Delete

    public void Delete(int id)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Slide obj = data.Slides.Where(p => p.Id == id).FirstOrDefault();
        if (obj != null)
          data.Slides.DeleteOnSubmit(obj);

        data.SubmitChanges();
      }
    }
  }
}
